package com.demo.las.controller;

import com.demo.las.dto.ApiResponse;
import com.demo.las.entity.StudyPlan;
import com.demo.las.service.StudyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/study-plans")
@CrossOrigin(origins = "*")
public class StudyPlanController {

    @Autowired
    private StudyPlanService studyPlanService;

    @GetMapping
    public ApiResponse<List<StudyPlan>> getAllStudyPlans(@RequestParam Long userId) {
        List<StudyPlan> plans = studyPlanService.getStudyPlansByUser(userId);
        return ApiResponse.success(plans);
    }

    @GetMapping("/active")
    public ApiResponse<List<StudyPlan>> getActiveStudyPlans(@RequestParam Long userId) {
        List<StudyPlan> plans = studyPlanService.getActiveStudyPlans(userId);
        return ApiResponse.success(plans);
    }

    @GetMapping("/{id}")
    public ApiResponse<StudyPlan> getStudyPlanById(@PathVariable Long id) {
        return studyPlanService.getStudyPlanById(id)
                .map(plan -> ApiResponse.success(plan))
                .orElse(ApiResponse.error(404, "学习计划不存在"));
    }

    @PostMapping
    public ApiResponse<StudyPlan> createStudyPlan(@Valid @RequestBody StudyPlan studyPlan) {
        try {
            StudyPlan createdPlan = studyPlanService.createStudyPlan(studyPlan);
            return ApiResponse.success("学习计划创建成功", createdPlan);
        } catch (Exception e) {
            return ApiResponse.error("创建学习计划失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ApiResponse<StudyPlan> updateStudyPlan(@PathVariable Long id, @Valid @RequestBody StudyPlan studyPlan) {
        try {
            studyPlan.setId(id);
            StudyPlan updatedPlan = studyPlanService.updateStudyPlan(studyPlan);
            return ApiResponse.success("学习计划更新成功", updatedPlan);
        } catch (Exception e) {
            return ApiResponse.error("更新学习计划失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteStudyPlan(@PathVariable Long id) {
        try {
            studyPlanService.deleteStudyPlan(id);
            return ApiResponse.success("学习计划删除成功", null);
        } catch (Exception e) {
            return ApiResponse.error("删除学习计划失败: " + e.getMessage());
        }
    }

    @GetMapping("/overdue")
    public ApiResponse<List<StudyPlan>> getOverduePlans(@RequestParam Long userId) {
        List<StudyPlan> overduePlans = studyPlanService.getOverduePlans(userId);
        return ApiResponse.success(overduePlans);
    }

    @GetMapping("/due-soon")
    public ApiResponse<List<StudyPlan>> getDueSoonPlans(@RequestParam Long userId) {
        List<StudyPlan> dueSoonPlans = studyPlanService.getDueSoonPlans(userId);
        return ApiResponse.success(dueSoonPlans);
    }

    @GetMapping("/progress")
    public ApiResponse<List<Object[]>> getPlanProgress(@RequestParam Long userId) {
        List<Object[]> progress = studyPlanService.getPlanProgressBySubject(userId);
        return ApiResponse.success(progress);
    }
}




